import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import electron from 'vite-plugin-electron'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig({
  css: {
    preprocessorOptions: {
      scss: {
        api: 'modern-compiler',
        silenceDeprecations: ['legacy-js-api']
      }
    }
  },
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    }),
    electron({
      entry: './electron/main.js'
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    port: 3000, // 你的前端应用将监听的端口
    //前端原来发出的url  http://127.0.0.1:3000/api/proxyTest
    proxy: {
      //当请求中带有/api 就会到代理服务器

      //  匹配到/api => 直接将ip+port部分转换为后端真实地址+端口=>得到的结果如下=> http://localhost:8888/api/proxyTest  (axios配置里面的baseUrl '/api' )+api.js里面请求的 '/proxyTest'

      '/api': {
        target: 'http://localhost:8080/',

        changeOrigin: true, // 允许跨域

        //之后路径重写 ：替换/api变为空  http://localhost:8888/proxyTest
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  }
})
